/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projettni;

/**
 *
 * @author dverstraete
 */
public class Utilitaires {

    static double calc_ln(double fLn1, double fUn1, double fPcum) {
        double fReturn = 0F;
        double fDelta = fUn1 - fLn1;
        fReturn = fLn1 + (fDelta * fPcum);
        return fReturn;
    }

    static double calc_un(double fLn1, double fUn1, double fPcum, double fPelem) {
        double fReturn = 0F;
        double fDelta = fUn1 - fLn1;
        fReturn = fLn1 + (fDelta * (fPcum + fPelem));
        return fReturn;
    }

    static int[] conversionPixelsImage(byte[] pixels){
        int[] tabPixels = new int[pixels.length];
        for(int i=0; i<pixels.length; i++){
            tabPixels[i] = pixels[i] + 128;
        }
        return tabPixels;
    }
    
    /**
     * Arrondi ou troncature
     * lower inclus, upper exclus [lower,upper[
     * @param tag
     * @param lower
     * @param upper
     * @return
     */
    public static byte[] tagToBinaire(double lower, double upper) {

        double tagPrime = 0F;
        int cpt = 1;
        String codeBinaireString = "";

        do {
            double tagTemp = tagPrime + Math.pow(0.5, cpt);
            if (tagTemp >= upper) {
                codeBinaireString += "0";
            } else {
                codeBinaireString += "1";
                tagPrime = tagTemp;
            }
            cpt++;
        } while (tagPrime < lower || tagPrime >= upper);

        System.out.println("Tag : " + tagPrime);

        //transformation en binaire
        char[] tabCodeChar = codeBinaireString.toCharArray();
        byte[] tabCodeBinaire = new byte[tabCodeChar.length];

        for (int i = 0; i < tabCodeBinaire.length; i++) {
            tabCodeBinaire[i] = (byte) ((tabCodeChar[i] == '1') ? 1 : 0);
        }
        return tabCodeBinaire;
    }

    /**
     * Convertit un tableau de binaires en nombre flottant
     * @param tab
     * @return
     */
    public static double binaireToTag(byte[] tab) {
        double value = 0F;
        for (int i = 0; i < tab.length; i++) {
            value += tab[i] * Math.pow(0.5, i + 1);
        }
        return value;
    }
}
